package com.youpai.platform.vulture.service.handler.netty;

import com.alibaba.fastjson.JSONObject;
import com.youpai.platform.common.exceptions.RemoteException;
import com.youpai.platform.vulture.common.entity.AccessMessage;
import com.youpai.platform.vulture.service.channel.VultureChannel;
import com.youpai.platform.vulture.service.handler.vulture.ConnectHandler;
import com.youpai.platform.vulture.service.handler.vulture.IVultureChannelHandler;
import com.youpai.platform.vulture.service.handler.vulture.MessageHandler;
import io.netty.channel.ChannelHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @date: 2022/7/9 22:33
 * @author: Anna
 * @version: 1.0
 * @description: 请求分发类
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class NettyChannelDispatchHandler implements IVultureChannelHandler {

    @Resource(name = "receiveMessageDispatchHandler")
    private MessageHandler receiveMessageHandler;
    @Resource(name = "disConnectedVultureChannelMessageHandler")
    private ConnectHandler disConnectedVultureChannelMessageHandler;
    @Resource(name = "connectedVultureChannelMessageHandler")
    private ConnectHandler connectedVultureChannelMessageHandler;

    @Override
    public void connect(VultureChannel vultureChannel) throws RemoteException {
        connectedVultureChannelMessageHandler.handle(vultureChannel);
    }

    @Override
    public void disConnect(VultureChannel vultureChannel) throws RemoteException {
        disConnectedVultureChannelMessageHandler.handle(vultureChannel);
    }

    @Override
    public void send(VultureChannel vultureChannel, AccessMessage accessMessage) throws RemoteException {
        log.info("sendMessage :{}", JSONObject.toJSONString(accessMessage));
    }

    @Override
    public void receive(VultureChannel vultureChannel, AccessMessage accessMessage) throws RemoteException {
        receiveMessageHandler.handle(vultureChannel, accessMessage);
    }

    @Override
    public void caught() throws RemoteException {

    }
}
